package com.fj.gateway.custom.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * @author fj
 * @date 2024/3/13 09:20
 */
@Component
public class LogGatewayFilterFactory extends AbstractGatewayFilterFactory<LogGatewayFilterFactory.Config> {

    public LogGatewayFilterFactory() {
        super(LogGatewayFilterFactory.Config.class);
    }

    @Override
    public GatewayFilter apply(Config config) {
        return (exchange, chain) -> {
            ServerHttpRequest request = exchange.getRequest();
            if (config.getOpenConsoleLog()){
                System.out.println("开启console打印");
            }
            if (config.getOpenFileLog()){
                System.out.println("开启file打印");
            }
            return chain.filter(exchange);
        };
    }

    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("openConsoleLog","openFileLog");
    }

    public static class Config{
        private Boolean openConsoleLog;
        private Boolean openFileLog;

        public Boolean getOpenConsoleLog() {
            return openConsoleLog;
        }

        public void setOpenConsoleLog(Boolean openConsoleLog) {
            this.openConsoleLog = openConsoleLog;
        }

        public Boolean getOpenFileLog() {
            return openFileLog;
        }

        public void setOpenFileLog(Boolean openFileLog) {
            this.openFileLog = openFileLog;
        }
    }
}
